package edu.towsom.cosc.cosc455.ksmith73;


public class MySyntaxAnalyzer implements SyntaxAnalyzer {

	private MyLexicalAnalyzer lexer;
	private MySemanticAnalyzer semantic;
	public Stack<String> parseTree = new Stack<String>();
	publc String currentToken;
	
	//Methods for all the different tags
	@Override
	public void document() throws CompilerException {

		if(Compiler.token.equalsIgnoreCase("#start document")){
			String message = "A #start document annotation was expected when a " + 
					Compiler.token + " was found!";
			throw new CompilerException(message);
		}
		
		lexer.getNextToken();
		
		body():
				
			
		if(Compiler.token.equalsIgnoreCase("#end document")){
			String message = "A #End Document annotation was expected when a " + 
					Compiler.token + " was found!";
			throw new CompilerException(message);
		}
			
	}
	
	public void body() throws CompilerException {
		// TODO Auto-generated method stub
		if(!(currentToken.equalsIgnoreCase("#end document"))){
			if(currentToken.equalsIgnoreCase("#start paragraph")){
				paragraph();
			    body();
			   	} 
			else if(currentToken.equalsIgnoreCase("#start bold")){
			    bold();
			    body();
			   } 
			else if(currentToken.equalsIgnoreCase("#start italics")){
			    italics();
			    body();
			   } 
			else if(currentToken.equalsIgnoreCase("#start list")){
			    list();
			    body();
			   } 
			else {
			    text("body");
			    body();
			   }
		}
}
	
	@Override
	public void head() throws CompilerException {
		// TODO Auto-generated method stub
		if(currentToken.equalsIgnoreCase("start head")){
			parseTree.push(currentToken);
			lexer.getNextToken();
			title();
			if(currentToken.equalsIgnoreCase("end head")){
				parseTree.push(currentToken);
				lexer.getNextToken();
			}
			else{
				error("Head needs to end.");
			}
		}
	}

	@Override
	public void title() throws CompilerException {
		// TODO Auto-generated method stub
		if(currentToken.equalsIgnoreCase("#start title")){
			parseTree.push(currentToken);
			lexer.getNextToken();
			if(!currentToken,charAt(0) == '#'){
				parseTree.push(currentToken);
				lexer.getNextToken();
				if(currentToken.equalsIgnoreCase("#end title")){
					parseTree.push(currentToken);
					lexer.getNextToken();
				}
				else {
					error("No title ending");
				}
				else {
					error("No text");
				}
			}
		}
	}

	@Override
	public void paragraph() throws CompilerException {
		// TODO Auto-generated method stub
		if(currentToken.equalsIgnoreCase("#start paragraph")){
			parseTree.push(currentToken);
			lexer.getNextToken();
			define();
			if(currentToken.equalsIgnoreCase("#end paragraph")){
				parseTree.push(currentToken);
				lexer.getNextToken
			}
			else {
				error("No paragraph ending");
			}
		}
	}

	@Override
	public void bold() throws CompilerException {
		// TODO Auto-generated method stub
		if(currentToken.equalsIgnoreCase("#start bold")){
			parseTree.push(currentToken);
			lexer.getNextToken();
			define();
			if(currentToken.equalsIgnoreCase("#end bold")){
				parseTree.push(currentToken);
				lexer.getNextToken();
			}
			else {
				error("No bold ending");
			}
		}
	}

	@Override
	public void italics() throws CompilerException {
		// TODO Auto-generated method stub
		if(currentToken.equalsIgnoreCase("#start italics")){
			parseTree.push(currentToken);
			lexer.getNextToken();
			define();
			if(currentToken.equalsIgnoreCase("#end italics")){
				parseTree.push(currentToken);
				lexer.getNextToken();
			}
			else {
				error("No italics ending");
			}
		}
	}

	@Override
	public void list() throws CompilerException {
		// TODO Auto-generated method stub
		if(currentToken.equalsIgnoreCase("#start list")){
			parseTree.push(currentToken);
			lexer.getNextToken();
			if(currentToken.equalsIgnoreCase("#start item")){
				parseTree.push(currentToken);
				lexer.getNextToken();
				define();
				item();
				if(currentToken.equalsIgnoreCase("#end item")){
					parseTree.push(currentToken);
					lexer.getNextToken();
					if(currentToken.equalsIgnoreCase("#end list")){
						parseTree.push(currentToken);
						lexer.getNextToken();
					}
					else {
						error("No list ending");
					}
				}
				else {
					error("No item ending");
				}
			}
			else {
				error("Need start list");
			}
		}
	}

	@Override
	public void item() throws CompilerException {
		// TODO Auto-generated method stub
		if(currentToken.equalsIgnoreCase("#start item")){
			parseTree.push(currentToken);
			lexer.getNextToken();
			define();
			if(currentToken.equalsIgnoreCase("#end item")){
				parseTree.push(currentToken);
				lexer.getNextToken();
			}
			else {
				error("No item ending");
			}
		}
	}

	@Override
	public void audio() throws CompilerException {
		// TODO Auto-generated method stub
		if(currentToken.equalsIgnoreCase("#start audio")){
			parseTree.push(currentToken);
		    lexer.getNextToken();
		    if(!(currentToken.charAt(0) == '#')){
		    	parseTree.push(currentToken);
		    	lexer.getNextToken();
		    	if(currentToken.equalsIgnoreCase("#end audio")){
		    		parseTree.push(currentToken);
		    		lexer.getNextToken();
		    	} 
		    	else {
		    		error("No audio ending");
		    	}
		    } 
		    else {
		    	error("No text");
		    }
		}
	}

	@Override
	public void video() throws CompilerException {
		// TODO Auto-generated method stub
		if(currentToken.equalsIgnoreCase("#start video")){
			parseTree.push(currentToken);
		    lexer.getNextToken();
		    if(!(currentToken.charAt(0) == '#')){
		    	parseTree.push(currentToken);
		    	lexer.getNextToken();
		    	if(currentToken.equalsIgnoreCase("#end video")){
		    		parseTree.push(currentToken);
		    		lexer.getNextToken();
		    	} 
		    	else {
		    		error("No video ending");
		    	}
		    } 
		    else {
		    	error("No text");
		    }
		}
	}

	@Override
	public void define() throws CompilerException {
		// TODO Auto-generated method stub
		if(currenttoken.equalsIgnoreCase("#start define")){
			parseTree.push(currentToken);
			lexer.getNextToken();
			if(currenttoken.equalsIgnoreCase("#name")){
				parseTree.push(currentToken);
				lexer.getNextToken();
				if(!(currentToken.charAt(0) == '#')){
					parseTree.push(currentToken);
				    lexer.getNextToken();
				    if(currenttoken.equalsIgnoreCase("#value")){
						parseTree.push(currentToken);
						lexer.getNextToken();
						if(!(currentToken.charAt(0) == '#')){
							parseTree.push(currentToken);
						    lexer.getNextToken();
						    if(currenttoken.equalsIgnoreCase("#end define")){
						    	define();
						    }
						    else ("No define ending");
						}
				    }
				    else {
				    	error("No text");
				    }
				}
				else {
					error("Need value");
				}
			}
			else {
				error("No text");
				}
		}
		else {
			error("Need name");
		}
	 }	

	@Override
	 public void use() throws CompilerException {
		  // TODO Auto-generated method stub
		  if(currenttoken.equalsIgnoreCase("#start use")){
			  parseTree.push(currentToken);
			  lexer.getNextToken();
			  if(!(currenttoken.charAt(0) == '#')){
				  parseTree.push(currentToken);
				  lexer.getNextToken();
				  if(currenttoken.equalsIgnoreCase("#end use")){
					  parseTree.push(currentToken);
					  lexer.getNextToken();
				  }		 	
				  else{
					  error("No use ending.");
				  }
			  }
			  else{
				  error("No text.");
			  }
		  }
		}

}
